package com.lishuang.services;

import com.lishuang.DAO.visitLogDAO.vLogDAOFactory;
import com.lishuang.DAO.visitLogDAO.visitLog;
import com.lishuang.domain.json.Device;
import org.jetbrains.annotations.NotNull;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * @author LiShuang
 * @version 1.0.0
 * @date 2022/11/18 9:13
 * @Description 演示实例使用！
 */
public class DAOService {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
    public static Timestamp submittime;

    private static Queue<Parallel> priorityQueue = new PriorityBlockingQueue<>();
    public static void submit(Device device) throws InterruptedException {
        priorityQueue.add(new Parallel(device,new Timestamp(new Date().getTime())));
        Thread.sleep(20);
    }
    public static Parallel fetch(){
        return priorityQueue.remove();
    }
    public static List<visitLog> start() throws Exception {
        vLogDAOFactory.getInstance().deleteAll();
        while(!priorityQueue.isEmpty()) {
            Parallel parallel = fetch();
            submittime = parallel.timestamp;
            vLogDAOFactory.getInstance().Insert(parallel.device);
        }
        return vLogDAOFactory.getInstance().findAll();
    }
}
class Parallel implements Comparable<Parallel>{
    Device device;
    Timestamp timestamp;

    public Parallel(Device device, Timestamp timestamp) {
        this.timestamp=timestamp;
        this.device=device;
    }

    @Override
    public int compareTo(@NotNull Parallel o) {
        return this.device.getDevicePriority()-o.device.getDevicePriority();
    }
}
